home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 16 / PC Actual CD 16.iso / cdactual / Os2 / 0509 / MacroExtension.java < prev    next >
Encoding:
Java Source  |  1997-08-19  |  4.6 KB  |  169 lines

  1. package macrolanguage;
  2. import sun.tools.javac.Main;
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import macrolanguage.SimpleStream;
  7.  
  8. /**
  9.  * To use the MacroExtension you simply add it to your program
  10.  * and add an interface for selecting the macro, then use the simple
  11.  * methods of this class to compile and execute a macro.
  12.  * The class offers several features:
  13.  * 1. Interaction with your application.
  14.  * 2. The full power you had writing the application will be available
  15.  *    to your users.
  16.  * 3. You can add a sandbox to block some functionality.
  17.  * 4. You can ship upgrades/fixes as plugins.
  18.  * 5. Other vendors and users can easily write plugins.
  19.  * 6. Plugins will be fully integrate, cross platform and run in the
  20.  *    same speed as the VM.
  21.  * It is recommended you expose some of your software's internals
  22.  * to your users to make this tool more effective.
  23. **/
  24. class MacroExtension extends ClassLoader
  25. {
  26.   MacroExtension()
  27.   {
  28.     compilerClass = new sun.tools.javac.Main(dataStream,"");
  29.   }
  30.  
  31.   /**
  32.    * This method accepts a class file as parameter
  33.    * and returns an instance of it.
  34.   **/
  35.   public Object getObject(String fileName)
  36.     throws  java.lang.ClassNotFoundException,
  37.             java.lang.InstantiationException,
  38.             java.lang.IllegalAccessException
  39.   {
  40.     Class classInstance = getClass();
  41.     return(classInstance.forName(fileName).newInstance());
  42.   }
  43.  
  44.   /**
  45.    * This method accepts a java file as parameter and compiles it.
  46.    * It returns true if compilation was successful and false otherwise.
  47.   **/
  48.   public boolean compile(String fileName)
  49.   {
  50.     String [] stringArr = new String[1];
  51.     stringArr[0] = fileName;
  52.     return(compilerClass.compile(stringArr));
  53.   }
  54.  
  55.   /**
  56.    * This method returns a vector in which all
  57.    * the compilation errors are stored as strings.
  58.   **/
  59.   public Vector getCompilationErrors()
  60.   {
  61.     return(dataStream.getStringsWritten());
  62.   }
  63.  
  64.   /**
  65.    * The main method is conveniently located here for testing purposes.
  66.   **/
  67.   public static void main(String argv[])
  68.   {
  69.     MacroExtension m = new MacroExtension();
  70.     m.compile("HelloWorld.java");
  71.     m.loadObject("HelloWorld");
  72.   }
  73.  
  74.   /**
  75.    * creates an instance of a class by getting its name
  76.    * and returns that instance, or null if none were found.
  77.   **/
  78.   public Object loadObject(String className)
  79.   {
  80.     try
  81.     {
  82.       loadClass(className, true);
  83.       return(getObject(className));
  84.     }
  85.     catch(java.lang.ClassNotFoundException e)
  86.     {
  87.       System.out.println("Class not found exception: " + e.getMessage());
  88.     }
  89.     catch(java.lang.InstantiationException e)
  90.     {
  91.       System.out.println("Instantiation exception: " + e.getMessage());
  92.     }
  93.     catch(java.lang.IllegalAccessException e)
  94.     {
  95.       System.out.println("Illegal access exception : " + e.getMessage());
  96.     }
  97.     catch(Exception e)
  98.     {
  99.       System.out.println("An exception was thrown: " + e.getMessage());
  100.     }
  101.     return(null);
  102.   }
  103.  
  104.  
  105.   protected Class loadClass(String className, boolean callReslove)
  106.   {
  107.     Class returnValue = (Class) classCache.get(className);
  108.  
  109.     if(returnValue != null)
  110.       return(returnValue);
  111.  
  112.     String classFullName;
  113.  
  114.     try
  115.     {
  116.       File classFile;
  117.       if (!className.endsWith(".class"))
  118.         classFullName = className + ".class";
  119.       else
  120.       {
  121.         className = className.substring(0, className.length() - 5);
  122.         System.out.println("Class name is: " + className);
  123.         classFullName = className + ".class";
  124.       }
  125.  
  126.       classFile = new File(classFullName);
  127.  
  128.       FileInputStream inFile = new FileInputStream(classFile);
  129.       byte[] classData = new byte[(int)classFile.length()];
  130.  
  131.       inFile.read(classData);
  132.  
  133.       inFile.close();
  134.  
  135.       returnValue = defineClass(className, classData, 0, classData.length);
  136.  
  137.       classCache.put(className, returnValue);
  138.     }
  139.     catch(IOException ioerr)
  140.     {
  141.       try
  142.       {
  143.         returnValue = findSystemClass(className);
  144.         return(returnValue);
  145.       }
  146.       catch (Exception anyException)
  147.       {
  148.         System.out.println("File access error: " + anyException.getMessage());
  149.         return(null);
  150.       }
  151.     }
  152.  
  153.     if(callReslove)
  154.     {
  155.       resolveClass(returnValue);
  156.     }
  157.  
  158.     return(returnValue);
  159.   }
  160.  
  161.   /**
  162.    * Notice the long name. It's here to avoid namespace
  163.    * problems with another Main
  164.    **/
  165.   private sun.tools.javac.Main compilerClass;
  166.   private SimpleStream dataStream = new SimpleStream();
  167.   private Hashtable classCache = new Hashtable();
  168. }
  169.